package com.dd.stack.mybatisplus.convertor;

import com.dd.stack.common.util.verification.DataVerificationUtil;
import com.dd.stack.mybatisplus.pojo.DO.DataObjectDO;
import com.dd.stack.mybatisplus.pojo.VO.DataObjectVO;
import com.dd.stack.mybatisplus.pojo.enumeration.DOTypeEnum;
import com.dd.stack.mybatisplus.pojo.req.DataObjectAddReq;
import com.dd.stack.mybatisplus.pojo.req.DataObjectUpdateReq;
import org.springframework.beans.BeanUtils;

import java.time.LocalDateTime;

/**
 * @Author liuxianmeng
 * @CreateTime 2025/1/8 23:13
 * @Description DataObject转换器
 */
public class DataObjectConvertor {

    /**
     * 将DataObjectUpdateReq转换为DataObjectDO
     */
    public static DataObjectDO updateReq2DO(DataObjectUpdateReq req) {
        DataObjectDO DO = new DataObjectDO();
        BeanUtils.copyProperties(req, DO);
        // 补充updatedAt属性
        DO.setUpdatedAt(LocalDateTime.now());
        return DO;
    }

    /**
     * 将DataObjectDO转换为DataObjectVO
     */
    public static DataObjectVO DO2VO(DataObjectDO DO) {
        DataObjectVO VO = new DataObjectVO();
        BeanUtils.copyProperties(DO, VO);
        return VO;
    }

    /**
     * 将DataObjectAddReq转换为DataObjectDO 用于向数据库插入数据
     */
    public static DataObjectDO convert2DataObjectDO(DataObjectAddReq req) {
        // 插入id为null 数据库自增
        DataObjectDO DO = DataObjectDO.builder().id(null).build();
        BeanUtils.copyProperties(req, DO);
        // 补充createdAt和updatedAt属性
        DO.setCreatedAt(LocalDateTime.now());
        DO.setUpdatedAt(LocalDateTime.now());
        // 枚举数据存储转换
        DO.setTypeEnumValue(DataVerificationUtil.parseEnum(DOTypeEnum.class, req.getUserType()));
        return DO;
    }
}
